package com.cyss.greenball.world;

import java.util.ArrayList;
import java.util.List;

import com.cyss.greenball.util.SystemUtil;
import com.lianzt.commondata.AbstractCommonData;

public class BallWorldFactory {

	public static BallWorld createBallWorldEmpty() {
		return new BallWorld();
	}

	public static BallWorld createBallWorldWithFrame() {
		BallWorld bw = new BallWorld();
		new RectangleBarrier(bw, 0.0f, 0.0f, 4.0f, SystemUtil.SCREEN_HEIGHT);
		new RectangleBarrier(bw, 0.0f, SystemUtil.SCREEN_HEIGHT - 4.0f,
				SystemUtil.SCREEN_WIDTH, SystemUtil.SCREEN_HEIGHT);
		new RectangleBarrier(bw, SystemUtil.SCREEN_WIDTH - 4.0f, 0.0f,
				SystemUtil.SCREEN_WIDTH, SystemUtil.SCREEN_HEIGHT);
		new RectangleBarrier(bw, 0.0f, 0.0f, SystemUtil.SCREEN_WIDTH, 4.0f);
		return bw;
	}

	public static BallWorld createBallWorldWithTest() {
		BallWorld bw = createBallWorldWithFrame();
		new Hole(bw, 70.0f, 300.0f, SystemUtil.GREEN_BALL_RADIUS + 2.0f,
				Hole.DESTINY);
		new Hole(bw, 70.0f, 250.0f, SystemUtil.GREEN_BALL_RADIUS + 2.0f,
				Hole.TRAP);
		new Hole(bw, 70.0f, 350.0f, SystemUtil.GREEN_BALL_RADIUS + 2.0f,
				Hole.TRAP);
		new Hole(bw, 20.0f, 350.0f, SystemUtil.GREEN_BALL_RADIUS + 2.0f,
				Hole.TRAP);
		new Hole(bw, 120.0f, 350.0f, SystemUtil.GREEN_BALL_RADIUS + 2.0f,
				Hole.TRAP);
		new LineBarrier(bw, 0, 150, 400, 190, 5);
		new LineBarrier(bw, 200, 180, 250, 120, 5);
		new GreenBall(bw, 70.0f, 20.0f, SystemUtil.GREEN_BALL_RADIUS);
		return bw;
	}

	public static BallWorld createBallWorldWithCommonData(AbstractCommonData acd) {
		BallWorld bw = createBallWorldEmpty();
		List<AbstractCommonData> lineBarrierList = acd
				.getArrayValue("lineBarrier");
		List<AbstractCommonData> recBarrierList = acd
				.getArrayValue("recBarrier");
		List<AbstractCommonData> holeList = acd.getArrayValue("hole");
		List<AbstractCommonData> greenBallList = acd.getArrayValue("greenBall");
		AbstractCommonData head = acd.getDataValue("head");
		int fromScreenHeight = head.getIntValue("screenHeight");
		int fromScreenWidth = head.getIntValue("screenWidth");
		float scaleX = (float) SystemUtil.SCREEN_WIDTH
				/ (float) fromScreenWidth;
		float scaleY = (float) SystemUtil.SCREEN_HEIGHT
				/ (float) fromScreenHeight;
		for (AbstractCommonData acdLine : lineBarrierList) {
			float startX = acdLine.getFloatValue("startX") * scaleX;
			float startY = acdLine.getFloatValue("startY") * scaleY;
			float endX = acdLine.getFloatValue("endX") * scaleX;
			float endY = acdLine.getFloatValue("endY") * scaleY;
			new LineBarrier(bw, startX, startY, endX, endY);
		}

		for (AbstractCommonData acdRec : recBarrierList) {
			float left = acdRec.getFloatValue("left") * scaleX;
			float top = acdRec.getFloatValue("top") * scaleY;
			float right = acdRec.getFloatValue("right") * scaleX;
			float bottom = acdRec.getFloatValue("bottom") * scaleY;
			new RectangleBarrier(bw, left, top, right, bottom);
		}

		for (AbstractCommonData acdHole : holeList) {
			float centerX = acdHole.getFloatValue("centerX") * scaleX;
			float centerY = acdHole.getFloatValue("centerY") * scaleY;
			new Hole(bw, centerX, centerY, SystemUtil.GREEN_BALL_RADIUS,
					acdHole.getIntValue("holeType"));
		}

		for (AbstractCommonData acdGreenBall : greenBallList) {
			float centerX = acdGreenBall.getFloatValue("centerX") * scaleX;
			float centerY = acdGreenBall.getFloatValue("centerY") * scaleY;
			new GreenBall(bw, centerX, centerY, SystemUtil.GREEN_BALL_RADIUS);
		}
		return bw;
	}

	public static BallWorld createBallWorldWithCommonDataAndFrame(
			AbstractCommonData acd) {
		BallWorld bw = createBallWorldWithCommonData(acd);
		new RectangleBarrier(bw, 0.0f, 0.0f, 4.0f, SystemUtil.SCREEN_HEIGHT);
		new RectangleBarrier(bw, 0.0f, SystemUtil.SCREEN_HEIGHT - 4.0f,
				SystemUtil.SCREEN_WIDTH, SystemUtil.SCREEN_HEIGHT);
		new RectangleBarrier(bw, SystemUtil.SCREEN_WIDTH - 4.0f, 0.0f,
				SystemUtil.SCREEN_WIDTH, SystemUtil.SCREEN_HEIGHT);
		new RectangleBarrier(bw, 0.0f, 0.0f, SystemUtil.SCREEN_WIDTH, 4.0f);
		return bw;
	}

}
